public class TestDemo {
    public static void main(String[] args) {
        System.out.println(myAtoi("2147483648"));
    }
    public static int myAtoi(String s) {
        char[] chars = s.toCharArray();
        int index = 0;
        while (index < chars.length && chars[index] == ' '){
            index++;
        }
        if (index == chars.length) return 0;
        int sign = 1;

        if (chars[index] == '+') {
            index++;
        }else if (chars[index] == '-') {
            sign = -1;
            index++;
        }



        int ret = 0;
        for (int i = index; i < chars.length; i++) {
            if (chars[i] >= '0' && chars[i] <= '9'){
                int num = chars[i] - 48;

                if (ret > Integer.MAX_VALUE / 10 || (ret == Integer.MAX_VALUE / 10 && num > Integer.MAX_VALUE % 10)) {
                    return Integer.MAX_VALUE;
                }
                if (ret < Integer.MIN_VALUE / 10 || (ret == Integer.MIN_VALUE / 10 && num > -(Integer.MIN_VALUE % 10))) {
                    return Integer.MIN_VALUE;
                }
                ret = ret*10 + num;
            }else{
                break;
            }
        }
        if (sign == -1) return -ret;
        return ret;
    }


    
}
